#!/bin/sh
#
# Laptop mode tools module: switch config files based on power state.
#

if [ x$CONTROL_CONFIG_FILES = x1 ] ; then
	log "MSG" "Swapping out configuration files."
	for CONFFILE in $CONFIG_FILES ; do
		if [ -e $CONFFILE ] ; then
			#
			# Initialize the -nolm-ac, -lm-ac and -batt files if they don't
			# already exist.
			#
			if [ ! -e "$CONFFILE-nolm-ac" ] ; then
				cp $CONFFILE $CONFFILE-nolm-ac
			fi
			if [ ! -e "$CONFFILE-lm-ac" ] ; then
				cp $CONFFILE $CONFFILE-lm-ac
			fi
			if [ ! -e "$CONFFILE-batt" ] ; then
				cp $CONFFILE $CONFFILE-batt
			fi

			#
			# Create the .lmbackup file
			#
			if [ "`readlink -f $CONFFILE`" != "$CONFFILE" ] ; then
				log "MSG" "$CONFFILE is a symlink."
				if [ ! -f "$CONFFILE.lmbackup" ] ; then
					log "MSG" "But there is no $CONFFILE.lmbackup."
					log "MSG" "Creating it now from $CONFFILE-nolm-ac."
					cp "$CONFFILE-nolm-ac" "$CONFFILE.lmbackup"
				fi
			else
				log "MSG" "$CONFFILE is not a symlink."
				if [ "$STATE" = "enabled" ] ; then
					log "MSG" "Saving it to $CONFFILE.lmbackup."
					cp --backup=numbered "$CONFFILE" "$CONFFILE.lmbackup"
				fi
			fi

			#
			# Swizzle the config file
			#
			if [ "$STATE" != "enabled" ] ; then
				log "MSG" "Laptop mode is not enabled. Restoring $CONFFILE."
				if [ -f "$CONFFILE.lmbackup" ] ; then
					mv "$CONFFILE.lmbackup" "$CONFFILE"
				elif [ "`readlink -f $CONFFILE`" != "$CONFFILE" ] ; then
					echo "ERROR: $CONFFILE is a symlink but $CONFFILE.lmbackup is not present."
				fi
			elif [ $ON_AC -eq 1 ] ; then
				if [ "$ACTIVATE" -eq 1 ] ; then
					log "MSG" "Pointing config file $CONFFILE to $CONFFILE-lm-ac."
					ln -fs "$CONFFILE-lm-ac" "$CONFFILE"
				else
					log "MSG" "Pointing config file $CONFFILE to $CONFFILE-nolm-ac."
					ln -fs "$CONFFILE-nolm-ac" "$CONFFILE"
				fi
			else
				log "MSG" "Pointing config file $CONFFILE to $CONFFILE-batt."
				ln -fs "$CONFFILE-batt" "$CONFFILE"
			fi
		fi
	done
	# Notify daemons of configuration change.
	for PROGRAM in $CONFIG_FILE_SIGNAL_PROGRAMS ; do	
		log "VERBOSE" "Sending SIGHUP to all $PROGRAM processes."
		killall -q -HUP $PROGRAM
	done


	if [ ! -z "$CONFIG_FILE_RELOAD_SERVICES" ] ; then
		# Determine how we can start/restart services.
		if ( which invoke-rc.d > /dev/null ) ; then
			# Debian uses invoke-rc.d
			RCPROG="invoke-rc.d "
			INITSCRIPT=laptop-mode
		elif ( which service > /dev/null ) ; then
			# RedHat uses service
			RCPROG="service "
			INITSCRIPT=laptop-mode
		else
			# Any other -- we start the init script it ourselves.

			# Try non-link directories first, then try links. This helps if one of
			# the locations is linked to another, which is the case on some distros.
			if [ -d /etc/rc.d/init.d -a ! -L /etc/rc.d/init.d ] ; then
				INIT_D=/etc/rc.d/init.d
			elif [ -d /etc/rc.d -a ! -L /etc/rc.d -a ! -d /etc/rc.d/init.d ] ; then
				INIT_D=/etc/rc.d
			elif [ -d /etc/init.d -a ! -L /etc/init.d ] ; then
				INIT_D=/etc/init.d
			elif [ -d /etc/rc.d/init.d ] ; then
				INIT_D=/etc/rc.d/init.d
			elif [ -d /etc/rc.d ] ; then
				INIT_D=/etc/rc.d
			elif [ -d /etc/init.d ] ; then
				INIT_D=/etc/init.d
			else
				log "VERBOSE" "Cannot determine location of init scripts."
				exit 1
			fi

			RCPROG="$INIT_D/"
		fi

		for SERVICE in $CONFIG_FILE_RELOAD_SERVICES ; do
			log "VERBOSE" "Reloading service $SERVICE."
			$RCPROG$SERVICE reload
		done
	fi
fi

